package com.unbank.util;

/**
 * 
 * @author 梁杨桃
 * 
 * 
 * 
 * */
public class UrlTools {

	public static void main(String[] args) {
		// http://finance.ce.cn/home/../rolling/201402/18/t20140218_2320510.shtml
		// http://www.ce.cn/culture/gd/201403/25/t20140325_2541605.shtml
		// http://www.ce.cn/culture/gd/201403/25/t20140325_2541605.shtml
		// http://www.ce.cn/culture/whcyk/gd/201403/25/t20140325_2541605.shtml
		// http://www.ce.cn/culture/whcyk/gd/201403/25/t20140325_2541605.shtml
		// http://www.ce.cn/culture/whcyk/gd/201403/25/t20140325_2541605.shtml

		// http://he.ce.cn/gd/201312/20/t20131220_1265799.shtml
		// http://he.ce.cn/gd/201312/20/t20131220_1265799.shtml
		// http://zdb.pedaily.cn/inv/show7928/
		// http://zdb.pedaily.cn/inv/show7928/
		System.out.println(getFullUrl("http://www.yanglee.com/product/product_Lists.aspx?ptype=105015016007",
				"product_detail.aspx?PID=100001535864449"));
	}

	// 根据根节点和 a标签的链接得到 a标签的标准的链接
	public static String getFullUrl(String url, String initurl) {
		String fullurl = null;
		String root = null;
		String uri = null;
		url = url.trim();
		initurl = initurl.trim().replace("\'", "");
		if (initurl == null || initurl.trim().isEmpty()) {
			return null;
		}
		if (initurl.startsWith("http") || initurl.startsWith("ftp")) {
			return initurl;
		} else {
			if (url.startsWith("http") || url.startsWith("ftp")) {
				String[] ss = url.split("//");
				if (!ss[1].contains("/")) {
					root = ss[0] + "//" + ss[1];
				} else {
					root = ss[0] + "//"
							+ ss[1].substring(0, ss[1].indexOf("/"));
				}
				root = root + "/";
			}
			uri = url.substring(0, url.lastIndexOf("/"));
			if (initurl.startsWith("../../../../")) {
				String temp = uri.substring(0, uri.lastIndexOf("/"));
				temp = temp.substring(0, temp.lastIndexOf("/"));
				temp = temp.substring(0, temp.lastIndexOf("/"));
				fullurl = temp.substring(0, temp.lastIndexOf("/"))
						+ initurl.substring(11);
			} else if (initurl.startsWith("../../../")) {
				String temp = uri.substring(0, uri.lastIndexOf("/"));
				temp = temp.substring(0, temp.lastIndexOf("/"));
				fullurl = temp.substring(0, temp.lastIndexOf("/"))
						+ initurl.substring(8);
			} else if (initurl.startsWith("../../")) {
				String temp = uri.substring(0, uri.lastIndexOf("/"));
				fullurl = temp.substring(0, temp.lastIndexOf("/"))
						+ initurl.substring(5);
			} else if (initurl.startsWith("../")) {
				fullurl = uri.substring(0, uri.lastIndexOf("/"))
						+ initurl.substring(2);
			} else if (initurl.startsWith("./")) {
				fullurl = uri + initurl.substring(1);
			} else if (initurl.startsWith("/")) {
				fullurl = root + initurl.substring(1);
			} else if (initurl.contains("javascript")) {
				return null;
			} else {
				fullurl = uri + "/" + initurl;
			}

		}
		return fullurl;
	}

}
